package  
{
	import org.flixel.*;
	
	/**
	 * ...
	 * @author J W
	 */
	public class PlayerTorso extends FlxSprite 
	{
		private var _hadPlayedJumpSound:Boolean = false;
		
		private var _falling:Boolean;
		private var _jump:Number;
		private var _runningBackwards:Boolean;
		private var _lastPressedRight:Boolean;
		private var _elVelocity:Number;				//variable assists in allowing player to jump from moving objects
		private var _jumpCounter:Number;			//variable used to prevent the idle jumping bug
		
		public function PlayerTorso(x:Number, y:Number):void
		{
			_falling = true;
			_runningBackwards = false;
			_lastPressedRight = false;
			
			super(x, y);
			
			loadGraphic(ImageFiles.playerTorso, true, true, 29, 39);
			addAnimation("idle", [0]);
			//addAnimation("run", [10, 11, 12, 13, 14, 15, 16, 17], 9);
			addAnimation("run", [1, 3, 4, 5], 9);
			addAnimation("runBackwards", [5, 4, 3, 1], 9);
			addAnimation("jump", [9]);
			
			maxVelocity.x = 180;
			maxVelocity.y = 450;
			acceleration.y = 1800;
			drag.x = maxVelocity.x * 5;
		}
		
		private function moveRight():void
		{
			acceleration.x = maxVelocity.x * 4;
		}
		
		private function moveLeft():void
		{
			acceleration.x = -maxVelocity.x * 4;
		}
		
		override public function update():void
		{
			//var jumpAcceleration:Number = 300;		//number used to control speed and height of jumps
			
			acceleration.x = 0;
			
			if (FlxG.keys.justPressed("A") || FlxG.keys.justPressed("LEFT"))
			{
				_lastPressedRight = false;
			}
			if (FlxG.keys.justPressed("D") || FlxG.keys.justPressed("RIGHT"))
			{
				_lastPressedRight = true;
			}
			
			if ((FlxG.keys.A || FlxG.keys.LEFT) && (FlxG.keys.D || FlxG.keys.RIGHT))
			{
				if (_lastPressedRight)
				{
					moveRight();
				}
				else 
				{
					moveLeft();
				}
			}
			else if (FlxG.keys.A || FlxG.keys.LEFT && !(FlxG.keys.justReleased("A") || FlxG.keys.justReleased("LEFT")))
			{
				//facing = FlxSprite.RIGHT;
				moveLeft();
			}
			else if (FlxG.keys.D || FlxG.keys.RIGHT && !(FlxG.keys.justReleased("D") || FlxG.keys.justReleased("RIGHT")))
			{
				//facing = FlxSprite.LEFT;
				moveRight();
			}
			
			if (this.velocity.x > -5 && this.velocity.x < 5 && !(FlxG.keys.A || FlxG.keys.LEFT) && !(FlxG.keys.D || FlxG.keys.RIGHT))
			{
				play("idle");
			}
			else
			{
				//Determines which running animation to play
				if (!_runningBackwards)
				{
					play("run");
				}
				else
				{
					play("runBackwards");
				}
			}
			
			//jump mechanics
			if ((FlxG.keys.justReleased("W") || FlxG.keys.justReleased("UP") || 
				FlxG.keys.justReleased("SPACE")) || (velocity.y > _elVelocity))	//prevents hovering by rapidly pressing jump			
				{
					_falling = true;
				}
			
			if (FlxG.keys.W || FlxG.keys.UP || FlxG.keys.SPACE)
			{
				if ((velocity.y != maxVelocity.y) && !_falling)	//control jump height
				{
					if (!_hadPlayedJumpSound)
					{
						FlxG.play(SoundFiles.jumpSound);
						_hadPlayedJumpSound = true;
					}
					
					velocity.y -= (FlxG.elapsed * (maxVelocity.y * 12));
					
					if (velocity.y <= -maxVelocity.y)
					{
						_falling = true;
						velocity.y = -maxVelocity.y;
					}
					
					if (!_hadPlayedJumpSound)
					{
						FlxG.play(SoundFiles.jumpSound);
						_hadPlayedJumpSound = true;
					}
				}
			}
			if (!onFloor)
			{
				if (_jumpCounter > 1)
				{
					play("jump");
				}
				_jumpCounter++;
			}
			else
			{
				_jumpCounter = 0;
			}
			
			super.update();
		}
		
		override public function hitBottom(Contact:FlxObject, Velocity:Number):void 
		{
			if (!FlxG.keys.W && !FlxG.keys.UP && !FlxG.keys.SPACE)
			{
				_hadPlayedJumpSound = false;
				_falling = false;
			}
			if (Contact.velocity.y >= 0)
			{
				_elVelocity = Contact.velocity.y * 1.2;
			}
			if (Contact.velocity.x > 0)
			{
				if (this.velocity.x == 0)
				{
					this.velocity.x = 0.01;
				}
			}
			if (Contact.velocity.x < 0)
			{
				if (this.velocity.x == 0)
				{
					this.velocity.x = -0.01;
				}
			}
			
			
            super.hitBottom(Contact, Velocity);
        }
		
		override public function hitTop(Contact:FlxObject, Velocity:Number):void
		{
			_falling = true;
			velocity.y = -velocity.y / 3;
			this.y += Contact.velocity.y / 20;
			
			super.hitTop(Contact, Velocity);
		}
		
		//Sets facing to LEFT and calls setRunningBackwards
		public function setFacingLeft():void
		{
			facing = FlxSprite.LEFT;
			setRunningBackwards();
		}
		
		//Sets facing to RIGHT and calls setRunningBackwards
		public function setFacingRight():void
		{
			facing = FlxSprite.RIGHT;
			setRunningBackwards();
		}
		
		/**
		 * Sets _runningBackwards based on player's facing and velocity
		 * called in setFacingLeft and setFacingRight
		 * 
		 * @param	runningBackwards		Determines if the character is moving backwards
		 */
		private function setRunningBackwards():void
		{
			if (_facing == RIGHT)
			{
				if (velocity.x < 0)
				{
					_runningBackwards = true;
				}
				else
				{
					_runningBackwards = false;
				}
			}
			else
			{
				if (velocity.x > 0)
				{
					_runningBackwards = true;
				}
				else
				{
					_runningBackwards = false;
				}
			}
		}
		
		//returns the value of _runningBackwards
		public function isRunningBackwards():Boolean
		{
			return _runningBackwards;
		}
		
		public function maintainScreenBounds(screenLeft:Number, screenRight:Number):void
		{
			if (this.x < screenLeft)
			{
				this.x = screenLeft;
				this.velocity.x = 0;
			}
			if (this.x + this.width > screenRight)
			{
				this.x = screenRight - this.width;
				this.velocity.x = 0;
			}
		}
		
		public function updateCollisionBounds():void
		{
			if (this.velocity.y >= 0)
			{
				colHullY.y = y + (1 * (height / 2)) - (velocity.y / 50);
				colHullY.height = (height / 2) + (velocity.y / 50);
			}
			else
			{
				colHullY.y = y;
				colHullY.height = height;
			}
			
			colHullX.x = this.x + 5;
			colHullX.width = this.width - 10;
			colHullY.x = this.x + 5;
			colHullY.width = this.width - 10;
		}
	}

}